package com.ehotting.edsta.circuitDesign.gen.circuit.script;

import com.alibaba.fastjson.JSON;
import com.ehotting.edsta.circuitDesign.domain.EsRuleScript;
import com.ehotting.edsta.circuitDesign.gen.circuit.helper.CircuitScriptHelper;
import com.ehotting.edsta.circuitDesign.gen.ScriptConstants;
import com.ehotting.edsta.circuitDesign.gen.element.bean.ConditionBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public abstract class CircuitScriptAbstract implements CircuitScriptAble {

    @Autowired
    private CircuitScriptHelper circuitScriptHelper;

    protected String functionStart(String functionName, List<String> argumentsList){
        StringBuffer buf = new StringBuffer();
        buf.append("def ");
        buf.append(circuitScriptHelper.genMethodNameAndArgument(functionName,argumentsList));
        buf.append("{"+ ScriptConstants.LINE);
        return buf.toString();
    }


    protected String functionEnd(){
        return "}";
    }


    /**
     * 根据priority从小到大排序
     * @param esScript
     * @return
     */
    protected List<ConditionBean> setConditionOrder(EsRuleScript esScript){
        List<ConditionBean> conditionList = JSON.parseArray(esScript.getContent(),ConditionBean.class);
        conditionList = conditionList.stream().sorted(Comparator.comparing(ConditionBean::getPriority,
                Comparator.nullsFirst(Integer::compareTo)))
                .collect(Collectors.toList());
        return conditionList;
    }


}
